var total_consume = 0;
var total_income = 0;

function SetupIconButtonEvent()
{
	$('.ui-state-default').hover(
		function(){ $(this).addClass('ui-state-hover'); }, 
		function(){ $(this).removeClass('ui-state-hover'); }
	);

	$('.ui-state-default').click(function(){
		$(this).toggleClass('ui-state-active');
	});
}

function DeleteAccounting( id )
{
	if( confirm("確定要刪除?") )
	{
		// send data
		$.getJSON(
			"backstage_delete_accounting.php",
			{
				auto_id: id
			},
			function( data )
			{
				// get data from fserver
				if( data.status == "success" )
				{
					QueryShowAccounting();
				}
				else
				{
					alert( data.error_msg );
				}
			}
		);
	}
}

function InitShowUserSelect()
{
	// start to add  user list options
	var str_array = new Array();
	for( var i=0; i<user_list.length; i++ )
	{
		str_array.push( "<option value=\"" );
		str_array.push( user_list[i].AutoID );
		str_array.push( "\">" );
		str_array.push( user_list[i].Name );
		str_array.push( "</option>" );
	}
	$("select#show_tab_user-select").html(str_array.join(''));
}

function ResetShowAccounting()
{
	// select the current user
	$("select#show_tab_user-select").val(user_id);
	
	// set range to today
	$("input#show_tab_range_start_date-text").datepicker( "setDate" , new Date() );
	$("input#show_tab_range_end_date-text").datepicker( "setDate" , new Date() );

	// default to month
	$("select#show_tab_query_type-select").val("month").change();
}

function InitShowYearSelect()
{
	var current_date = new Date();
	var current_year = current_date.getFullYear();
	
	// add option
	var str_array = new Array();
	for( var i=smallest_year; i<=current_year; i++ )
	{
		str_array.push( "<option value=\"" );
		str_array.push( i );
		str_array.push( "\">" );
		str_array.push( i );
		str_array.push( "</option>" );
	}
	$("select#show_tab_query_month_year-select").html(str_array.join('')).val(current_year);
	$("select#show_tab_query_year_year-select").html(str_array.join('')).val(current_year);
}

function InitShowMonthSelect()
{
	var current_date = new Date();
	var current_month = current_date.getMonth() + 1;
	
	// add option
	var str_array = new Array();
	for( var i=1; i<=12; i++ )
	{
		str_array.push( "<option value=\"" );
		str_array.push( i );
		str_array.push( "\">" );
		str_array.push( i );
		str_array.push( "</option>" );
	}
	$("select#show_tab_query_month_month-select").html(str_array.join('')).val(current_month);
}

function QueryShowAccounting()
{
	// date type
	var date_type = $("select#show_tab_query_type-select").val();
	var start_date = ""; var start_date_in_date;
	var end_date = ""; var end_date_in_date;
	
	// get date
	if( date_type == "month" )
	{
		var prefix = $("select#show_tab_query_month_year-select").val() + "-" + $("select#show_tab_query_month_month-select").val();
		start_date = prefix + "-01";
		end_date = prefix + "-31";
	}
	else if( date_type == "year" )
	{
		var prefix = $("select#show_tab_query_year_year-select").val();
		start_date = prefix + "-01-01";
		end_date = prefix + "-12-31";
	}
	else if( date_type == "range" )
	{
		start_date = $("input#show_tab_range_start_date-text").val();
		start_date_in_date = new Date(start_date);
		end_date = $("input#show_tab_range_end_date-text").val();
		end_date_in_date = new Date(end_date);
		if( start_date_in_date > end_date_in_date )
		{
			alert("End date must be bigger than start date.");
			$("input#show_tab_range_end_date-text").focus();
			return;
		}
	}

	// show overlay
	$("div#loading_overlay-div").show();

	// send data
	$.get(
		"backstage_show_accounting.php",
		{
			user_id: $("select#show_tab_user-select").val(),
			start_date: start_date,
			end_date: end_date
		},
		function( dataInString )
		{
			// find the last "}"
			var end_pos = dataInString.lastIndexOf("}");
			
			// get substring
			valid_dataInString = dataInString.substr(0, end_pos+1);
			
			// real json data
			data = $.parseJSON(valid_dataInString);
			
			// get data from fserver
			if( data.status == "success" )
			{
				RefreshShowAccountingConsumeTable( data.accounting_consume_list );
				RefreshShowAccountingIncomeTable( data.accounting_income_list, data.accounting_total );
				SetupIconButtonEvent();
			}
			else
			{
				alert( data.error_msg );
			}
			
			// hide overlay
			$("div#loading_overlay-div").hide();
		}
	);
	
	
	/*
	$.getJSON(
		"backstage_show_accounting.php",
		{
			user_id: $("select#show_tab_user-select").val(),
			start_date: start_date,
			end_date: end_date
		},
		function( data )
		{
			// get data from fserver
			if( data.status == "success" )
			{
				RefreshShowAccountingConsumeTable( data.accounting_consume_list );
				RefreshShowAccountingIncomeTable( data.accounting_income_list, data.accounting_total );
				SetupIconButtonEvent();
			}
			else
			{
				alert( data.error_msg );
			}
			
			// hide overlay
			$("div#loading_overlay-div").hide();
		}
	);
	*/
}

function RefreshShowAccountingConsumeTable( accounting_consume_list )
{
	var str_array = new Array();
	total_consume = 0;
	for( var i=0; i<accounting_consume_list.length; i++ )
	{
		str_array.push( "<tr>" );
		str_array.push( "<td>" );
		str_array.push( accounting_consume_list[i].Date );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( field_mapping[accounting_consume_list[i].FieldID] );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( accounting_consume_list[i].Description );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( accounting_consume_list[i].Amount );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( "<div class=\"ui-state-default ui-corner-all\" title=\"刪除\" onClick=\"DeleteAccounting('" );
		str_array.push( accounting_consume_list[i].AutoID );
		str_array.push( "');\"><span class=\"ui-icon ui-icon-trash\"></span></div>" );
		str_array.push( "</td>" );
		str_array.push( "</tr>" );
		
		// calculate total
		total_consume += parseInt(accounting_consume_list[i].Amount);
	}
		
	// add total consume row
	str_array.push( "<tr>" );
	str_array.push( "<td>總計:</td>" );
	str_array.push( "<td colspan=\"3\">" );
	str_array.push( total_consume );
	str_array.push( "</td>" );
	str_array.push( "</tr>" );
	
	$("table#show_acounting_consume_list-table tbody").html(str_array.join(''));
}

function RefreshShowAccountingIncomeTable( accounting_income_list, data_accounting_total )
{
	var str_array = new Array();
	total_income = 0;
	for( var i=0; i<accounting_income_list.length; i++ )
	{
		str_array.push( "<tr>" );
		str_array.push( "<td>" );
		str_array.push( accounting_income_list[i].Date );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( field_mapping[accounting_income_list[i].FieldID] );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( accounting_income_list[i].Description );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( accounting_income_list[i].Amount );
		str_array.push( "</td>" );
		str_array.push( "<td>" );
		str_array.push( "<div class=\"ui-state-default ui-corner-all\" title=\"刪除\" onClick=\"DeleteAccounting('" );
		str_array.push( accounting_income_list[i].AutoID );
		str_array.push( "');\"><span class=\"ui-icon ui-icon-trash\"></span></div>" );
		str_array.push( "</td>" );
		str_array.push( "</tr>" );

		// calculate total
		total_income += parseInt(accounting_income_list[i].Amount);
	}
	
	// add total income row
	str_array.push( "<tr>" );
	str_array.push( "<td>總計:</td>" );
	str_array.push( "<td>" + total_income + "</td>" );
	str_array.push( "<td>本月結餘:</td>" );
	str_array.push( "<td colspan=\"2\">" + (total_income - total_consume) + "</td>" );
	str_array.push( "</tr>" );
	
	// add total
	str_array.push( "<tr>" );
	str_array.push( "<td>總結餘:</td>" );
	str_array.push( "<td colspan=\"4\">" + data_accounting_total + "</td>" );
	str_array.push( "</tr>" );
	

	$("table#show_acounting_income_list-table tbody").html(str_array.join(''));
}

function InitShowAccounting()
{
	InitShowUserSelect();
	InitShowYearSelect();
	InitShowMonthSelect();
	
	// setup select change
	$("select#show_tab_query_type-select").change(function(){
		$("tr#condition_month-tr, tr#condition_year-tr, tr#condition_range-tr").hide();
		if( $(this).val() == "month" ) $("tr#condition_month-tr").show();
		else if( $(this).val() == "year" ) $("tr#condition_year-tr").show();
		else $("tr#condition_range-tr").show();
		
		// do query
		QueryShowAccounting();
	});

	// reset show accounting
	ResetShowAccounting();

	// if anything changes, do query
	$("select#show_tab_user-select, select#show_tab_query_month_year-select, \
		select#show_tab_query_month_month-select, select#show_tab_query_year_year-select, \
		input#show_tab_range_start_date-text, show_tab_range_end_date-text").change(function(){
		QueryShowAccounting();
	});
}
